<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
      @load="onLoad"
    >
      <van-cell v-for="(article,index) in list" :key="index" :title="article.title" />
    </van-list>
  </div>
</template>

<script>
import {getSearchResult} from '@/api/search'
export default {
  name: 'SearchResult',
  components: {},
  props: {
      searchText: {
        type: String,
        require: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page: 1,
	  perPage: 20,
      error: false,
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
   async onLoad () {
       try{
           //  1.请求数据
            const {data} = await  getSearchResult({
                page: this.page, // 页码
                per_page: this.perPage, // 每页大小
                q: this.searchText // 搜索关键字
            })
            // 2.将数据添加到列表
            const {results}=data.data
            this.list.push(...results)
            // 3.设置加载状态结束
            this.loading=false
            // 4判断是否还加载
            if (results.length) {
                this.page++//如果有就获取下一页数据
            }else {
                this.finished=true//没有数据了就将加载状态设置结束 不在onLoad
            }
       }catch(err){
           this.error=true
        //    加载失败loading关闭
        this.loading=false
       }
    
    
    }
  }
}
</script>

<style scoped lang="less"></style>
